package com.invite.me.ui.registeractivity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.invite.me.R;
import com.invite.me.base.BaseActivity;
import com.invite.me.beans.presenter.RegisterPresenter;
import com.invite.me.beans.res.YanZhenMaShouBean;
import com.invite.me.beans.res.YanZhenmaDlShouBean;
import com.invite.me.ui.home.HomeActivity;
import com.invite.me.util.PhoneFormatCheckUtils;
import com.invite.me.util.SspPrefUtils;
import com.invite.me.util.ToastUtils;
import com.invite.me.wigets.CountDownTextView;
import com.invite.me.wlqq.Constants;
import com.invite.me.wlqq.Contract;

public class RegisterActivity extends BaseActivity implements View.OnClickListener, Contract.BaseView{

    private EditText phone;
    private EditText yanzhengma;
    private CountDownTextView yanzhengmasend;
    RegisterPresenter registerPresenter;

    @Override
    protected void initView() {
        registerPresenter=new RegisterPresenter(this);
        TextView pawasswordregister = findViewById(R.id.pawasswordregister);
        TextView denglu = findViewById(R.id.denglu);
        TextView enroll = findViewById(R.id.enroll);
        phone = findViewById(R.id.phone);
        yanzhengma = findViewById(R.id.yanzhengma);
        yanzhengmasend = findViewById(R.id.yanzhengmasend);
        TextView gorgetpawwword = findViewById(R.id.gorgetpawwword);

        pawasswordregister.setOnClickListener(this);
        enroll.setOnClickListener(this);
        denglu.setOnClickListener(this);
        yanzhengmasend.setOnClickListener(this);
        gorgetpawwword.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pawasswordregister:
                Intent pwr=new Intent(RegisterActivity.this,PasswordRegisterActivity.class);
                startActivity(pwr);

                break;
            case R.id.enroll:
                Intent rnb=new Intent(RegisterActivity.this,RegisterNumberActivity.class);
                startActivity(rnb);

                break;
            case R.id.denglu:
//                SspPrefUtils.setString(Constants.SSPTOKEN,"856b26a01472259107a7728593a09620");
//                String string = SspPrefUtils.getString(Constants.SSPTOKEN, "");
                //   {"code":1000,"message":"登录成功","data":{"userId":"16da85befdd20fa16f27bdf3ea91e1ad"}}
//                SspPrefUtils.setString(Constants.SSPTOKEN,"6ac2486a1bcbb7197609b96798857ac3");
                //   SspPrefUtils.setString(Constants.SSPTOKEN,"43d5af11a386066e246a321f3098b8e4");//被邀约者
//                Intent dl=new Intent(RegisterActivity.this,HomeActivity.class);
//                startActivity(dl);
//                Intent rnbb=new Intent(RegisterActivity.this,ShipinBofang.class);
//                startActivity(rnbb);


                String dengluphone = phone.getText().toString();
                String yanzhengmayz = yanzhengma.getText().toString();
                boolean phonedl = PhoneFormatCheckUtils.isPhoneLegal(dengluphone);
                if(phonedl){
                    if(!TextUtils.isEmpty(yanzhengmayz)){
                        registerPresenter.denglu(dengluphone,yanzhengmayz);
                    }else {
                        ToastUtils.show(RegisterActivity.this, "请输入验证码", Toast.LENGTH_SHORT);
                    }

                }else {
                    ToastUtils.show(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT);
                }



                break;
            case R.id.yanzhengmasend:
                String phones = phone.getText().toString();
                if(!TextUtils.isEmpty(phones)){
                    boolean phoneLegal = PhoneFormatCheckUtils.isPhoneLegal(phones);
                    if (phoneLegal) {
                        registerPresenter.yanzhengma(phones,"2");
                        yanzhengmasend.setCountDownEndText("重新发送")
                                .setCountingTextColor(
                                        ContextCompat.getColor(this, R.color.yanzhengmah)
                                )
                                .setCountEndTextColor(ContextCompat.getColor(this, R.color.yanzhengmah))

                                .setOnCountDownListener(new CountDownTextView.OnCountDownListener() {
                                    @Override
                                    public void onCountDown(int allTime, int remain) {
                                        if (remain <= 0) {
                                            yanzhengmasend.setClickable(true);
                                            yanzhengmasend.setEnabled(true);
                                        }
                                    }
                                })
                                .startCountDown();
                    }else {
                        ToastUtils.show(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT);

                    }

                }else {
                    ToastUtils.show(RegisterActivity.this,"请输入手机号");
                }


                break;
            case R.id.gorgetpawwword:
                //重设置密码
                Intent chongzhi=new Intent(RegisterActivity.this,RegisterCsmmActivity.class);
                startActivity(chongzhi);



                break;
        }

    }

    @Override
    protected int getActivityLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void loadData() {

    }


    //网络请求
    @Override
    public void setPresenter(Contract.BasePresenter basePresenter) {
        if (registerPresenter instanceof Contract.BasePresenter) {
            this.registerPresenter = (RegisterPresenter) basePresenter;
        }
    }

    @Override
    public void onDataSucess(Object o) {
        if(o instanceof YanZhenMaShouBean){
            YanZhenMaShouBean yanZhenMaShouBean= (YanZhenMaShouBean) o;
//            String code = yanZhenMaShouBean.getData().getCode();
//            yanzhengma.setText(code);
        }else if(o instanceof YanZhenmaDlShouBean){
            YanZhenmaDlShouBean yanZhenmaDlShouBean= (YanZhenmaDlShouBean) o;
            if(yanZhenmaDlShouBean.getCode()==1000){

                if(yanZhenmaDlShouBean.getData()!=null){
                    String userId = yanZhenmaDlShouBean.getData().getUserId();
                    if(!TextUtils.isEmpty(userId)&&userId.length()>0){
                        SspPrefUtils.setString(Constants.SSPTOKEN,userId);
                        //名字
                        String nickname = yanZhenmaDlShouBean.getData().getNickname();
                        //头像
                        String headerImgPath = yanZhenmaDlShouBean.getData().getHeaderImgPath();
                        if(!TextUtils.isEmpty(nickname)){
                            SspPrefUtils.setString(Constants.NAKNAME,nickname);
                        }
                        if(!TextUtils.isEmpty(headerImgPath)){
                            SspPrefUtils.setString(Constants.TOUXIANG,headerImgPath);
                        }

                        Intent dl=new Intent(RegisterActivity.this,HomeActivity.class);
                        startActivity(dl);
                        finish();
                    }else{
                        ToastUtils.show(RegisterActivity.this,"请先注册再登录");
                    }
                }else {
                    ToastUtils.show(RegisterActivity.this,yanZhenmaDlShouBean.getMessage());
                }

            }else {
                ToastUtils.show(RegisterActivity.this,yanZhenmaDlShouBean.getMessage());
            }




        }

    }

    @Override
    public void onDataFaile(Object o) {

    }

    @Override
    public void onComplete() {

    }
}